package com.oig.sys.config;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.util.JsonUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@Getter
@Setter
@ToString
@Slf4j
@ConfigurationProperties(prefix = JpaAuditProperties.AUDIT_PREFIX)
public class JpaAuditProperties implements InitializingBean  {

    public static final String AUDIT_PREFIX = "audit.jpa";


    private Boolean logEnabled = Boolean.TRUE;
    private Boolean autoCallEnabled = Boolean.FALSE;
    private List<String> listenTables ;
    private List<String> excludeTables ;

    public boolean isAudit(String table) {
        //log.debug("判断表：{} 是否开启审计",table);
        if (CollectionUtil.isNotEmpty(excludeTables) && excludeTables.contains(table)) {
            log.debug("表：{} 未开启审计",table);
            return false;
        }
        if (CollectionUtil.isEmpty(listenTables)) {
            return true;
        }
        return listenTables.contains(table);
    }


    @Override
    public void afterPropertiesSet() {
        log.info("审计配置信息：{}", JsonUtil.toJSONString(this));
    }
}
